﻿using DG.Tweening;
using EventManager;
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using XCharts.Runtime;
using static UnityEngine.EventSystems.EventTrigger;

public class UIRight : MonoBehaviour
{
    public static UIRight instance;

    [Tooltip("帮助Tog")]
    public Toggle help_Tog;
    public Button closeHelp_Btn;
    public Transform helpView_Transf;
    public Vector3 endPostion_V3;
    public Vector3 startPostion_V3;
    internal float helpCSSpeed = 60f;

    public List<Transform> dataView_List = new List<Transform>();
    //public List<BaseChart> baseCharts_List = new List<BaseChart>();
    private int viewIndexOld = 0;//上次打开的界面（需关闭的界面）
    internal Vector3 endPoint_V3 = new Vector3(431, 0, 0);


    #region 设备告警

    #endregion



    #region 车间级右侧面板
    [Title("原料消耗")]
    [FoldoutGroup("车间级")]
    public Toggle recentlyCJ_Tog_csu;//原料消耗近日Toggle（consume）；
    [FoldoutGroup("车间级")]
    public Toggle historyCJ_Toggle_csu;//原料消耗历史Toggle;
    [FoldoutGroup("车间级")]
    public List<GameObject> Cj_obj_cus;
    [FoldoutGroup("车间级")]
    public List<BarChart> _allBarchartCJ_cus;
    [Title("车间级原料耗能历史原料消耗统计")]
    [FoldoutGroup("车间级")]
    public List<BaseChart> _allBarChartCJ_csu;
    [FoldoutGroup("车间级")]
    public List<Toggle> _allToggleCJ_csu;//车间切换Toggle( raw material)
    [FoldoutGroup("车间级")]
    public List<TextMeshProUGUI> rma_Text_csu;
    [FoldoutGroup("车间级")]
    public List<float> rma_aluminumCJ_int;//精铝车间对应数据
    [FoldoutGroup("车间级")]
    public List<float> rma_castCJ_int;//铸造车间对应数据
    [FoldoutGroup("车间级")]
    public List<float> rma_materialCJ_int;//原料制备车间对应数据
    [FoldoutGroup("车间级")]
    public List<GameObject> rma_objCJ_list;//近日切换状态obj


    [Title("车间任务")]
    [FoldoutGroup("车间级")]
    public Toggle recentlyCJ_Tog_task;//车间任务近日Toggle；
    [FoldoutGroup("车间级")]
    public Toggle historyCJ_Tog_task;//车间任务历史Toggle;
    [Title("历史界面和近日界面")]
    [FoldoutGroup("车间级")]
    public List<GameObject> _renandhisCJ_list_task;
    [FoldoutGroup("车间级")]
    [Title("近日任务情况Slider")]
    [FoldoutGroup("车间级")]
    public Slider aluminumCJ_Sdr;//车间任务精铝车间slidder；
    [FoldoutGroup("车间级")]
    public Slider castCJ_Sdr;//车间任务原料制备车间slidder；
    [FoldoutGroup("车间级")]
    public Slider materialCJ_Sdr;//车间任务铸造车间slidder；
    [FoldoutGroup("车间级")]
    public List<TextMeshProUGUI> _allCJ_workshop_Text;
    [Title("近日任务情况Barchart")]
    [FoldoutGroup("车间级")]
    public List<BaseChart> _BatchartCJ_list_task;
    [Title("近日车间选择按钮")]
    [FoldoutGroup("车间级")]
    public List<Toggle> __allTogCJ_task;


    [Title("车间生产合格率")]
    [FoldoutGroup("车间级")]
    public Toggle recentlyCJ_Tog_Pr;//车间生产合格率近日Tog（Pass rate）
    [FoldoutGroup("车间级")]
    public Toggle historyCJ_Tog_Pr;//车间生产合格率历史Tog（Pass rate）
    [FoldoutGroup("车间级")]
    public List<GameObject> _renandhisCj_list_Pr;
    [Title("车间生产车间选择按钮")]
    [FoldoutGroup("车间级")]
    public List<Toggle> _allTogglesCJ_Pr;//车间生产车间选择按钮
    [Title("车间生产不同车间折线图")]
    [FoldoutGroup("车间级")]
    public List<LineChart> _allLineChartCj_Pr;//车间生产不同车间折线图

    #endregion


    #region 工厂级右侧面板
    [Title("任务监控")]
    [Title("任务监控近日历史Toggle")]
    [FoldoutGroup("工厂级")]
    public List<Toggle> monitor_recandhisGc_list;
    [FoldoutGroup("工厂级")]
    [Header("近日历史切换界面")]
    public List<GameObject> obj_monitorGc_list;


    #endregion 产线级右侧面板
    [Title("产线耗能")]
    [Title("历史耗能统计Toggle")]
    [FoldoutGroup("产线级")]
    public List<Toggle> _ecpCj_tog;   //energy consumption
    [Title("历史耗能统计对应图表")]
    [FoldoutGroup("产线级")]
    public List<BaseChart> _ecpBarCj_list;   //energy consumption

    public List<BaseChart> NotAddedChart = new List<BaseChart>();

    private void Awake()
    {
        instance = this;
    }



    void Start()
    {
       
        #region //车间级右侧


        #region //车间级消耗
        //车间原料消耗历史切换按钮
        ToggleEventManager.Toggle_AddListener_OBJ(recentlyCJ_Tog_csu, Cj_obj_cus, 0);
        //近日按钮刷新图标对应关系 0130
        recentlyCJ_Tog_csu.onValueChanged.AddListener((b) => {
            if (b)
            {
                for (int i = 0; i < _allToggleCJ_csu.Count; i++)
                {
                    if (_allToggleCJ_csu[i].isOn)
                    {
                        ToggleEventManager.ObjShow(rma_objCJ_list, i, true);
                    }
                }
            }

        });

        ToggleEventManager.Toggle_AddListener_OBJ(historyCJ_Toggle_csu, Cj_obj_cus, 1);
        //历史按钮刷新图标对应关系 0130
        historyCJ_Toggle_csu.onValueChanged.AddListener((b) => {
            if (b)
            {
                for (int i = 0; i < _allToggleCJ_csu.Count; i++)
                {
                    if (_allToggleCJ_csu[i].isOn)
                    {
                        ToggleEventManager.BarShow(_allBarChartCJ_csu, i, b);
                    }
                }
            }

        });
        //车间Toggle切换按钮
        ToggleEventManager.Toggle_AddListener(_allToggleCJ_csu[0], null);
        _allToggleCJ_csu[0].onValueChanged.AddListener((bl) =>
        {
           
            if (bl)
            {
                if (recentlyCJ_Tog_csu.isOn) 
                {
                    Debug.Log("aaaaaa");
                    //ToggleEventManager.Toggle_AddListener_OBJ(_allToggleCJ_csu[0], rma_objCJ_list, 0);
                    ToggleEventManager.ObjShow(rma_objCJ_list, 0, true);

                }
                if (historyCJ_Toggle_csu.isOn)
                {
                    ToggleEventManager.BarShow(_allBarChartCJ_csu,0, bl);
                    
                }
               
            }
        });
        ToggleEventManager.Toggle_AddListener(_allToggleCJ_csu[1], null);
        _allToggleCJ_csu[1].onValueChanged.AddListener((bl) =>
        {
            

            if (bl)
            {
                if (recentlyCJ_Tog_csu.isOn)
                {
                    Debug.Log("bbbbbbb");
                    //ToggleEventManager.Toggle_AddListener_OBJ(_allToggleCJ_csu[1], rma_objCJ_list, 1);
                    ToggleEventManager.ObjShow(rma_objCJ_list, 1, true);
                }
                if (historyCJ_Toggle_csu.isOn)
                {
                    ToggleEventManager.BarShow(_allBarChartCJ_csu, 1, bl);

                }

            }
        });
        ToggleEventManager.Toggle_AddListener(_allToggleCJ_csu[2], null);
        _allToggleCJ_csu[2].onValueChanged.AddListener((bl) =>
        {
           

            if (bl)
            {
                if (recentlyCJ_Tog_csu.isOn)
                {
                    Debug.Log("ccccccc");
                    //ToggleEventManager.Toggle_AddListener_OBJ(_allToggleCJ_csu[2], rma_objCJ_list, 2);
                    ToggleEventManager.ObjShow(rma_objCJ_list, 2, true);

                }
                if (historyCJ_Toggle_csu.isOn)
                {
                    ToggleEventManager.BarShow(_allBarChartCJ_csu, 2, bl);

                }

            }
        });
        #endregion

        #region//车间任务
        //历史近日按钮切换
        ToggleEventManager.Toggle_AddListener_OBJ(recentlyCJ_Tog_task, _renandhisCJ_list_task, 0);
        ToggleEventManager.Toggle_AddListener_OBJ(historyCJ_Tog_task, _renandhisCJ_list_task, 1);

        //近日车间按钮切换
        ToggleEventManager.Toggle_AddListener_Bar(__allTogCJ_task[0], _BatchartCJ_list_task, 0);
        ToggleEventManager.Toggle_AddListener_Bar(__allTogCJ_task[1], _BatchartCJ_list_task, 1);
        ToggleEventManager.Toggle_AddListener_Bar(__allTogCJ_task[2], _BatchartCJ_list_task, 2);
        //
        #endregion

        #region
        //车间生产合格率
        //近日历史切换按钮
        ToggleEventManager.Toggle_AddListener_OBJ(recentlyCJ_Tog_Pr, _renandhisCj_list_Pr, 0);
        ToggleEventManager.Toggle_AddListener_OBJ(historyCJ_Tog_Pr, _renandhisCj_list_Pr, 1);
        //车间生产车间选择按钮
        ToggleEventManager.Toggle_AddListener_Lin(_allTogglesCJ_Pr[0], _allLineChartCj_Pr, 0);
        ToggleEventManager.Toggle_AddListener_Lin(_allTogglesCJ_Pr[1], _allLineChartCj_Pr, 1);
        ToggleEventManager.Toggle_AddListener_Lin(_allTogglesCJ_Pr[2], _allLineChartCj_Pr, 2);


        #endregion





        #endregion


        #region //工厂级右侧
        ToggleEventManager.Toggle_AddListener_OBJ(monitor_recandhisGc_list[0], obj_monitorGc_list, 0);
        ToggleEventManager.Toggle_AddListener_OBJ(monitor_recandhisGc_list[1], obj_monitorGc_list, 1);


        #endregion




        #region  //产线级右侧
        ToggleEventManager.Toggle_AddListener_Bar(_ecpCj_tog[0], _ecpBarCj_list, 0);
        ToggleEventManager.Toggle_AddListener_Bar(_ecpCj_tog[1], _ecpBarCj_list, 1);
        ToggleEventManager.Toggle_AddListener_Bar(_ecpCj_tog[2], _ecpBarCj_list, 2);






        #endregion

        #region 添加监听
        help_Tog.onValueChanged.AddListener((b) => 
        {
            if (b)
            ShowHelpView();
        });
        #endregion
        //query_IF.onEndEdit.AddListener((str) => { });
        dataView_List[0].gameObject.SetActive(LoginView.loginVeiwActive);
    }
    //访问下标
    public int getindex
    {
        get { return viewIndexOld; }
    }
    public void ShowHelpView() 
    {
        helpView_Transf.SetAsLastSibling();
        MotionManager.instance.MoveObj(helpView_Transf, endPostion_V3, helpCSSpeed, () =>
        {
            Debug.Log("帮助界面打开");
        });
    }

    public void CloseHelpView()
    {
        help_Tog.isOn= false;
        MotionManager.instance.MoveObj(helpView_Transf, startPostion_V3, helpCSSpeed, () => 
        {
            Debug.Log("帮助界面关闭");
        });
    }

    
    internal Coroutine dataViewChang_Cor;
    public void DataViewChang(int viewIndex_int)
    {
        if (dataViewChang_Cor != null)
        {
            StopCoroutine(dataViewChang_Cor);
            if (dataView_List[viewIndexOld].localPosition != Vector3.zero && dataView_List[viewIndexOld].localPosition != endPoint_V3)
            {
                dataView_List[viewIndexOld].localPosition = endPoint_V3;
            }
        }
        viewIndexOld = viewIndex_int;
        dataViewChang_Cor = StartCoroutine(DataViewChanged(viewIndex_int));
    }

    public static  int olderIndex;
    private IEnumerator DataViewChanged(int viewIndex_int)
    {
        for (int i = 0; i < dataView_List.Count; i++)
        {
            if (dataView_List[i].transform.localPosition == Vector3.zero)
            {
                yield return MotionManager.instance.MoveObj_IE(dataView_List[i], endPoint_V3, 25f);
                ToggleEventManager._Play_anima(_BatchartCJ_list_task);
                ToggleEventManager._Play_anima(_ecpBarCj_list);
                ToggleEventManager._Play_anima(NotAddedChart);
            }
        }
        yield return MotionManager.instance.MoveObj_IE(dataView_List[viewIndex_int], Vector3.zero, 25f, () =>
        {
            
        });
        olderIndex = viewIndex_int;
    }

    bool str=true;
    public void DataViewChanged_end(int viewIndex_int)
    {
        if (str)
        {
            MotionManager.instance.MoveObj(dataView_List[viewIndex_int], endPoint_V3, 25f, () =>
             {
                 str = false;
             });
             
        }
        else if (!str)
        {
            MotionManager.instance.MoveObj(dataView_List[viewIndex_int], Vector3.zero,25f, () =>
            {
                str = true;
            });
            

        }

    }
    //播放图标初始动画
    public void playLin(LineChart lineChart)
    {
       lineChart.AnimationReset();
       lineChart.AnimationFadeIn();
    }



    //故障列表查询
    public void QueryAlarm(string query_str)
    {

    }

    public void OnUI() 
    {
        dataView_List[0].localPosition = endPoint_V3;
        dataView_List[0].gameObject.SetActive(true);

        for (int i = 0; i < 2; i++)
        {
            //baseCharts_List[i].gameObject.SetActive(false);
        }

        Tween tween = dataView_List[0].DOLocalMoveX(0,1);
        tween.OnComplete(() =>
        {
            for (int i = 0; i < 2; i++)
            {
                //baseCharts_List[i].gameObject.SetActive(true);
                //baseCharts_List[i].AnimationReset();

                //baseCharts_List[i].AnimationFadeIn();
                UIDown.instance.allUIParent_GameObj[0].gameObject.SetActive(true);
            }
        });
    }
}

